package com.jbp.job;

import com.jbp.model.Room;
import com.jbp.redis.RedisConfigService;
import com.jbp.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**人气值衰减*/
@Component
@Configuration
public class PopTask {
    @Autowired
    private RoomService roomService;

    @Value("${runType}")
    private String runType;
    @Autowired
    private RedisConfigService redisConfigService;

    @Scheduled(fixedDelay = 1000)
    private void popCutTasks() {
        if (redisConfigService.setLock("pop_room_mood", 60)) {
            List<Room> roomList = roomService.list();
            if (roomList == null || roomList.size() == 0) {
                return;
            }
            //批量修改人气值
            roomList = roomList.stream().map(list -> {
                //房间固定人气值
                if (list.getMood() > 100000000) {
                    list.setMood(list.getMood() - 10000000);
                } else if (list.getMood() > 10000000) {
                    list.setMood(list.getMood() - 1000000);
                } else if (list.getMood() > 1000000) {
                    list.setMood(list.getMood() - 100000);
                } else if (list.getMood() > 500000) {
                    list.setMood(list.getMood() - 50000);
                } else if (list.getMood() > 100000) {
                    list.setMood(list.getMood() - 10000);
                } else if (list.getMood() > 50000) {
                    list.setMood(list.getMood() - 5000);
                } else if (list.getMood() > 20000) {
                    list.setMood(list.getMood() - 2000);
                } else if (list.getMood() > 10000) {
                    list.setMood(list.getMood() - 1000);
                } else if (list.getMood() > 4000) {
                    list.setMood(list.getMood() - 400);
                } else {
                    list.setMood(list.getMood() - 200);
                }
                if (list.getMood() < 0) {
                    list.setMood(0);
                }
                return list;
            }).collect(Collectors.toList());

            roomService.updateBatch(roomList);
        }
    }
}
